#include<vector>
#include<iostream>
using namespace std;
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> vv;
        sort(nums.begin(), nums.end());
        vv.reserve(300);
        for (int i = 0; i < nums.size(); i++) {
            if (i > 0 && nums[i - 1] == nums[i])continue;
            if (nums[i] > 0)break;
            int left = i + 1;
            int right = nums.size() - 1;
            while (left < right) {
                if (nums[left] + nums[right] > -nums[i])right--;
                else if (nums[left] + nums[right] < -nums[i])left++;
                else {
                    vv.push_back({ nums[i],nums[left],nums[right] });
                    left++, right--;
                    while (left < right && nums[left - 1] == nums[left])left++;
                    while (left < right && nums[right + 1] == nums[right])right--;
                }
            }
        }
        return vv;
    }
};